package com.gogo.mall.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.gogo.mall.entity.QyMember;
import com.gogo.mall.dao.QyMemberDao;
import com.gogo.mall.result.ResponseCode;
import com.gogo.mall.result.ResponseData;
import com.gogo.mall.service.QyMemberService;
import com.gogo.mall.utils.JwtUtil;
import com.gogo.mall.utils.Ossutils.ConstPropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Member;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * (QyMember)表服务实现类
 *
 * @author makejava
 * @since 2021-08-27 14:34:39
 */
@Service("qyMemberService")
public class QyMemberServiceImpl implements QyMemberService {
    @Resource
    private QyMemberDao qyMemberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QyMember queryById(Integer id) {
        return this.qyMemberDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<QyMember> queryAllByLimit(int offset, int limit) {
        return this.qyMemberDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param qyMember 实例对象
     * @return 实例对象
     */
    @Override
    public QyMember insert(QyMember qyMember) {
        this.qyMemberDao.insert(qyMember);
        return qyMember;
    }

    /**
     * 修改数据
     *
     * @param qyMember 实例对象
     * @return 实例对象
     */
    @Override
    public QyMember update(QyMember qyMember) {
        this.qyMemberDao.update(qyMember);
        return this.queryById(qyMember.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.qyMemberDao.deleteById(id) > 0;
    }

    @Override
    public boolean checkTel(String tel) {
        //正则匹配手机号
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(tel);
        boolean ok = matcher.matches();
        return ok;
    }

    @Override
    public boolean queryByTel(QyMember member) {
        if(qyMemberDao.queryAll(member).size()>0)
            return false;
        return true;
    }

    @Override
    public ResponseData loginCheck(QyMember memberInfo) {
        List<QyMember> qyMembers = qyMemberDao.queryAll(memberInfo);
        if(qyMembers.size()<=0)
            return new ResponseData("0","账号或密码错误");
        String telphone = memberInfo.getTelphone();
        String password = memberInfo.getPassword();
        QyMember member = qyMembers.get(0);

        if(telphone.equals(member.getTelphone())&&password.equals(member.getPassword())){
            //登录成功，生成token 30分钟有效期
            String token = JwtUtil.createJwt(1800, member);
            return new ResponseData("1","登录成功",token);
        }
        return new ResponseData("0","账号或密码错误");
    }

    @Override
    public String uploadImg(MultipartFile file) throws IOException {
        String endpoint = ConstPropertiesUtils.EndPoint;
        String accessKeyId = ConstPropertiesUtils.KeyId;
        String accessKeySecret= ConstPropertiesUtils.KeySecret;
        String buckName= ConstPropertiesUtils.BucketName;
        String fileName=file.getOriginalFilename();
        InputStream inputStream=file.getInputStream();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        fileName="mall/"+uuid+fileName;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(buckName,fileName, inputStream);
        ossClient.shutdown();
        String url=null;
        url="https://"+buckName+'.'+endpoint+'/'+fileName;
        return url;
    }
}
